<template>
  <div class="add">
    <!-- 
      el-dialog
        visible: 控制是否显示弹框
        .sync: 控制点右上角的x关闭
        slot="default/title/footer"

      表单验证: 
        1: el-form ref model rules
        2: el-form-item prop="字段名"
        rules: {
          prop字段名: [
            {required:true, message: 错误信息, trigger: 'blur/change'},
            {min: 最小长度, max: 最大长度, messsage, trigger: 'blur/change'},
            {validator: (rule, value, callback) => {
              成功: callback()
              失败: callback(new Error(错误信息))
            }, trigger: 'blur/change'}
          ]
        }
        this.$refs.ref值.validate(result => {
          if(result) {
            // 验证通过
          }
        })
        this.$refs.ref值.validateField(['prop的值'], errorMessage => {
          验证成功: errorMessage === ''
          验证不成功: errorMessage !== ''
        })
    -->
    <el-dialog class="dialog" :visible.sync="isShow" title="新增组织架构">
      <el-form
        :model="form"
        :rules="rules"
        label-position="right"
        ref="form"
        label-width="120px"
        class="form"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input v-model.trim="form.name"></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager">
            <el-option
              v-for="(item, index) in allUser"
              :key="index"
              :label="item.username"
              :value="item.username"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            type="textarea"
            :rows="6"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="footer">
          <el-button>取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysUserSimple } from '@/api/departments'
export default {
  props: ['allDepts'],
  data () {
    return {
      isShow: false,
      allUser: [],
      treeObj: '',
      // 表单数据
      form: {
        name: '',
        code: '',
        introduce: '',
        manager: '',
        pid: ''
      },
      // 表单规则, required是否必填, message未输入提示, validator自定义校验规则
      // trigger触发规则, blur 焦点事件, change 内容变化事件
      rules: {
        name: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              // 1. 找出同级部门, 通过 id 找同级, 非树形结构
              // 2. 同级部门对比, 如果存在: callback(new Error(部门名重复))
              //                如果不存在: callback()
              const _arr = this.allDepts.filter((item, index) => {
                if (item.pid === this.treeObj.id) {
                  return true
                }
              })
              let _index = _arr.findIndex((item, index) => {
                return item.name === value
              })
              _index === -1
                ? callback()
                : callback(new Error('请不要添加重复的部门名称'))
            }
          }
        ],
        code: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入1-300个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入内容', trigger: 'change' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'change' }
        ]
      }
    }
  },

  created () {
    // 监听
    this.$bus.$on('showDialog', (bol, treeObj) => {
      this.isShow = bol
      this.treeObj = treeObj
    })
    this.getAllUser()
  },
  methods: {
    async getAllUser () {
      this.allUser = await sysUserSimple()
    },
    // 确定点击
    async submit () {
      try {
        const res = await this.$refs.form.validate()
        if (res) {
          this.$message({
            type: 'success',
            message: '验证成功'
          })
        }
      } catch (err) {
        this.$message({
          type: 'error',
          message: '验证失败'
        })
      }
    }
  },
  beforeDestroy () {
    this.$bus.$off('showDialog')
  }
}
</script>

<style lang="scss" scoped>
.dialog {
  .footer {
    text-align: center;
  }
}
</style>
